package com.cph.mail;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * https://blog.csdn.net/yimcarson/article/details/84936440?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task
 * spring.mail.port不指定；spring.mail.password不是邮箱密码，需要登录mail.163.com，
 * 前往设置 客户端授权密码中获取的一个16个字符的密码，同时允许POP3/SMTP服务。
 */
@RestController
@Slf4j
public class MailController {

    @Autowired
    private JavaMailSender jms;


    @GetMapping("/send")
    public String send(){

        //建立邮件消息
        SimpleMailMessage mainMessage = new SimpleMailMessage();
        //发送者
        mainMessage.setFrom("18518492202@163.com");
        //接收者
        mainMessage.setTo("919101386@qq.com");
        //发送的标题
        mainMessage.setSubject("嗨喽！");
        //发送的内容
        mainMessage.setText("hello world");
        jms.send(mainMessage);
        return "1";
    }
//
//    /* *
//     * @Description  http://localhost:8888/sendMail
//     * @author dalaoyang
//     * @email 397600342@qq.com
//     * @method 发送文本邮件
//     * @date
//     * @param
//     * @return
//     */
//    @RequestMapping("/sendMail")
//    public String sendMail() {
//        SimpleMailMessage message = new SimpleMailMessage();
//        message.setFrom(sender);
//        message.setTo(receiver);
//        message.setSubject("大老杨");
//        message.setText("你好你好你好！");
//        try {
//            javaMailSender.send(message);
//            logger.info("简单邮件已经发送。");
//        } catch (Exception e) {
//            logger.error("发送简单邮件时发生异常！", e);
//        }
//        return "success";
//    }
//
//    /* *
//     * @Description  http://localhost:8888/sendHtmlMail
//     * @author dalaoyang
//     * @email 397600342@qq.com
//     * @method 发送html邮件
//     * @date
//     * @param
//     * @return
//     */
//    @RequestMapping("/sendHtmlMail")
//    public String testHtmlMail() {
//        String content="<html>\n" +
//                "<body>\n" +
//                "    <h3>hello world ! 这是一封Html邮件!</h3>\n" +
//                "</body>\n" +
//                "</html>";
//        MimeMessage message = javaMailSender.createMimeMessage();
//
//        try {
//            //true表示需要创建一个multipart message
//            MimeMessageHelper helper = new MimeMessageHelper(message, true);
//            helper.setFrom(sender);
//            helper.setTo(receiver);
//            helper.setSubject("html mail");
//            helper.setText(content, true);
//
//            javaMailSender.send(message);
//            logger.info("html邮件发送成功");
//        } catch (MessagingException e) {
//            logger.error("发送html邮件时发生异常！", e);
//        }
//        return "success";
//    }
//
//    /* *
//     * @Description http://localhost:8888/sendFilesMail
//     * @author dalaoyang
//     * @email 397600342@qq.com
//     * @method 发送附件邮件
//     * @date
//     * @param
//     * @return
//     */
//    @RequestMapping("/sendFilesMail")
//    public String sendFilesMail() {
//        String filePath="/Users/dalaoyang/Downloads/article_tag.sql";
//        MimeMessage message = javaMailSender.createMimeMessage();
//
//        try {
//            MimeMessageHelper helper = new MimeMessageHelper(message, true);
//            helper.setFrom(sender);
//            helper.setTo(receiver);
//            helper.setSubject("附件邮件");
//            helper.setText("这是一封带附件的邮件", true);
//
//            FileSystemResource file = new FileSystemResource(new File(filePath));
//            String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
//            helper.addAttachment(fileName, file);
//
//            javaMailSender.send(message);
//            logger.info("带附件的邮件已经发送。");
//        } catch (MessagingException e) {
//            logger.error("发送带附件的邮件时发生异常！", e);
//        }
//        return "success";
//    }
//
    /* *
     * @Description http://localhost:8888/sendInlineResourceMail
     * @author dalaoyang
     * @email 397600342@qq.com
     * @method 发送图片邮件
     * @date
     * @param
     * @return
     */
    @RequestMapping("/sendInlineResourceMail")
    public String sendInlineResourceMail() {
        String Id = "dalaoyang12138";
        String content="<html><body>这是有图片的邮件：<img src=\'cid:" + Id + "\' ></body></html>";
        String imgPath = "C:\\Users\\86185\\Desktop\\111.jpg";
        MimeMessage message = jms.createMimeMessage();

        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom("18518492202@163.com");
            helper.setTo("18518492202@163.com");
            helper.setSubject("这是有图片的邮件");
            helper.setText(content, true);

            FileSystemResource res = new FileSystemResource(new File(imgPath));
            helper.addInline(Id, res);

            jms.send(message);
            log.info("嵌入静态资源的邮件已经发送。");
        } catch (MessagingException e) {
            log.error("发送嵌入静态资源的邮件时发生异常！", e);
        }
        return "success";
    }
}